<?php

namespace App\Http\Controllers\Seller;

use App\Http\Controllers\Controller;
use App\Qingwuit\Models\Order;
use App\Qingwuit\Models\OrderSettlement;
use App\Qingwuit\Models\Riders;
use Illuminate\Http\Request;
class RidersController extends Controller
{
    protected $modelName = 'Riders'; #对应Laravel 生成的 Model文件


//司机填写配送时间
    public function fill(Request $request){

        $order_id = $request->order_id??'';
        $time = $request->time??'';
        $order = Order::find($order_id);
        if($order){
            $order->deliverytime = $time;
            $order->save();
            return response()->json(['message' => '修改成功']);
        }
            return response()->json(['message' => '修改失败'], 500);
    }

    //更改订单状态接口
        public function changeStatus(Request $request)
    {

        $user_id = $this->getUserId('users');
        $riders = Riders::where('user_id',$user_id)->first();
//        print_r($riders);die;
//        if (empty($riders->name)){
//            return $this->error('请补全信息');
//        }
        $deliverytime = $request->deliverytime??'';
        $deliverytimes = date('Y-m-d H:i:s');
        $deliverytime = strtotime($deliverytimes);
        $status   = $request->status??'';
        $order_id   = $request->order_id??'';
        $code   = $request->code??'';
        if (!$code) return $this->error('取件码不能为空');
        if ($status == 2){
            $order = Order::where('pickup_code',$code)->first();
            if($order){
                if ($order->riders){
                    return $this->error('订单已经被接走了');
                }
                if ($code != $order->pickup_code){
                    return $this->error('取件码错误');
                }
                $order->order_status = 3;
                $order->riders = $user_id;
                $order->deliverytime = $deliverytime;
                $order->save();
                return $this->success('修改成功');
            }else{
                return $this->error('订单不存在');
            }
        }

        if ($status == 3){
            $order = Order::where('id',$order_id)->first();
            if($order){
                if ($code != $order->legal_code){
                    return $this->error('送货码错误');
                }
                $order->order_status = 6;
                $order->receipt_time = time();
                $order->save();
                return $this->success('修改成功');
            }else{
                return $this->error('订单不存在');
            }
        }

        return $this->error('修改失败');
    }

    public function index(Request $request)
    {
        try {
            $rs = $this->getService($this->serviceName)
                ->setUserConfig(['setUser'=>($this->isSuper?false:$this->setUser),'auth'=>$this->auth,'belongName'=>$this->belongName])
                ->getPageData($this->modelName);
            return $this->handle($rs);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }



}
